#include "teacherloginsuccess.h"
#include "ui_teacherloginsuccess.h"
#include <iostream>
#include <string>
#include <QString>
#include <QMessageBox>
using namespace std;

TeacherLoginSuccess::TeacherLoginSuccess(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TeacherLoginSuccess)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/img/teacher.png"));
    this->pasi = new AddStudentInfo;
    this->pusi = new UpdateStudentInfo;
    pms = ManagementSystem::GetInstance();
    this->myTitle = "  id     姓名        学号       年级       身份证号      已修学分  绩点  用户名     密码\n";
}


TeacherLoginSuccess::~TeacherLoginSuccess()
{
    pms->SaveToFile();
    delete ui;
}

// 点击返回主界面按钮
void TeacherLoginSuccess::on_back2main_clicked()
{
    emit back2main();
    this->hide();
}

void TeacherLoginSuccess::on_back2login_clicked()
{
    emit back2login();
    this->hide();
}

void TeacherLoginSuccess::on_showAll_clicked()
{
    // 清除上一次显示结果
    ui->showBox->clear();
    // 获取复选框排序规则
    int curIndex = ui->sortRule->currentIndex();
    cout << "choose: " << curIndex << endl;
    // 获取选择框（选中代表从低到高排序）
    bool isAscd = ui->isAscd->isChecked() == true;
    if (isAscd)    {        cout << "hit" << endl;    }
    else    {        cout << "unhit" << endl;    }
    vector<StudentInfo*> vStu;
    switch (curIndex) {
    case STUID:
        vStu = pms->rbStuId->getVec();
        break;
    case SCORE:
        vStu = pms->rbScore->getVec();
        break;
    case CREDIT:
        vStu = pms->rbCredit->getVec();
        break;
    case USERNAME:
        vStu = pms->rbUsername->getVec();
        break;
    default:
        break;
    }
    cout << "current total:" << vStu.size() << endl;
    if (!isAscd)
    {
        reverse(vStu.begin(), vStu.end());
    }
    ui->showBox->insertPlainText(myTitle);
    int id = 1;
    for (auto it = vStu.begin(); it != vStu.end(); ++it, ++id)
    {
        ui->showBox->insertPlainText(QString::fromStdString(numExtend(id,4)));
        ui->showBox->insertPlainText(QString::fromStdString((*it)->sformat()));
    }
}


// 清除显示框内容
void TeacherLoginSuccess::on_clearAll_clicked()
{
    ui->showBox->clear();
}

// 对int类型数字格式化，格式化为长度为len字符串，前面空格补齐
string TeacherLoginSuccess::numExtend(int n, int len)
{
    string cur = to_string(n);
    int curlen = cur.size();
    int dist = len - curlen;
//    cout << len << "-" << curlen << "=" << dist << endl;
    if (dist > 0)
    {
        string space(dist, ' ');
        return space + cur;
    }
    else if (dist < 0)
    {
        cout << cur << "Error: string too long" << endl;
        exit(0);
    }
    return cur;
}


// 点击添加信息按钮
void TeacherLoginSuccess::on_addInfo_clicked()
{
    this->pasi->show();
}

// 点击右上角关闭窗口
void TeacherLoginSuccess::closeEvent(QCloseEvent *e){
    //窗口关闭时弹出的提示窗口
    QMessageBox msgBox;
    msgBox.setWindowTitle("提示");
    msgBox.setWindowIcon(QIcon(":/img/teacher.png"));
    msgBox.setText("关闭自动保存到磁盘");
    msgBox.setInformativeText("确认退出?");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok){
        //若用户点击确认，则接收这个事件,当前窗口会关闭
        e->accept();
        ManagementSystem::GetInstance()->SaveToFile();
    }else{
        //若用户点击取消，则忽略这个事件，当前窗口不会关闭
        e->ignore();
    }
}

// 点击查询、修改、删除按钮
void TeacherLoginSuccess::on_submit_clicked()
{
    // 查找0 修改1 删除2
    int curIndex = ui->opType->currentIndex();
    switch(curIndex)
    {
    case 0:
        searchInfo();
        break;
    case 1:
        updateInfo();
        break;
    case 2:
        deleteInfo();
        break;
    }
}

// 查询操作
void TeacherLoginSuccess::searchInfo()
{
    int mode = ui->opWay->currentIndex();
    StudentInfo dst;
    StudentInfo* ret = nullptr;
    if (mode == 0)
    {
        cout << "choose search by stuId" << endl;
        string inputId = ui->editLine->text().toStdString();
        cout << "test1" << inputId << endl;
        dst.setStuId(inputId);
        ret = pms->rbStuId->search(&dst);
        if (ret)
        {
            cout << "loc:" << ret << endl;
            ui->showBox->setText(QString::fromStdString(ret->sformat()));
            ui->showBox->insertPlainText(myTitle);
            ui->showBox->insertPlainText("   1");

        }
        else
        {
            cout << "search failed" << endl;
            QMessageBox::critical(this,
                   tr("搜索失败"),
                   tr("输入学号不存在"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
    else if (mode == 1)
    {
        cout << "choose search by username" << endl;
        string inputUsername = ui->editLine->text().toStdString();
        dst.setUsername(inputUsername);
        ret = pms->rbUsername->search(&dst);
        if (ret)
        {
            cout << "loc:" << ret << endl;
            ui->showBox->setText(QString::fromStdString(ret->sformat()));
            ui->showBox->insertPlainText(myTitle);
            ui->showBox->insertPlainText("  1");
        }
        else
        {
            cout << "search failed" << endl;
            QMessageBox::critical(this,
                   tr("搜索失败"),
                   tr("输入用户名不存在"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
}

// 修改操作
void TeacherLoginSuccess::updateInfo()
{
    int mode = ui->opWay->currentIndex();
    StudentInfo dst;
    StudentInfo* ret = nullptr;
    // 获取需要修改的学生信息指针
    if (mode == 0)
    {
        cout << "choose update by stuId" << endl;
        string inputId = ui->editLine->text().toStdString();
        dst.setStuId(inputId);
        ret = pms->rbStuId->search(&dst);
        if (!ret)
        {
            cout << "search failed" << endl;
            QMessageBox::question(this,
                   tr("搜索失败"),
                   tr("输入学号不存在"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
    else if (mode == 1)
    {
        cout << "choose update by username" << endl;
        string inputUsername = ui->editLine->text().toStdString();
        dst.setUsername(inputUsername);
        ret = pms->rbUsername->search(&dst);
        if (!ret)
        {
            cout << "search failed" << endl;
            QMessageBox::question(this,
                   tr("搜索失败"),
                   tr("输入用户名不存在"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
    if (ret) // 获取成功
    {
        cout << "loc:" << ret << endl;
        connect(this, SIGNAL(sendInfo(StudentInfo*)), pusi, SLOT(getInfo(StudentInfo*)));
        emit sendInfo(ret);
        pusi->show();
    }
}

// 删除操作
void TeacherLoginSuccess::deleteInfo()
{
    int mode = ui->opWay->currentIndex();
    StudentInfo dst;
    StudentInfo* ret = nullptr;
    if (mode == 0)
    {
        cout << "choose delete by stuId" << endl;
        string inputId = ui->editLine->text().toStdString();
        dst.setStuId(inputId);
        ret = pms->rbStuId->search(&dst);
        if (ret)
        {
            cout << "loc:" << ret << endl;
            cout << pms->remove(ret) << endl;
            ui->showBox->setText(QString::fromStdString("学号为" + inputId + "的学生已被成功删除"));
        }
        else
        {
            cout << "search failed" << endl;
            QMessageBox::critical(this,
                   tr("删除失败"),
                   tr("输入学号不存在"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
    else if (mode == 1)
    {
        cout << "choose delete by username" << endl;
        string inputUsername = ui->editLine->text().toStdString();
        dst.setUsername(inputUsername);
        ret = pms->rbUsername->search(&dst);
        if (ret)
        {
            cout << "loc:" << ret << endl;
            cout << pms->remove(ret) << endl;
            ui->showBox->setText(QString::fromStdString("用户名为" + inputUsername + "的学生已被成功删除"));
        }
        else
        {
            cout << "search failed" << endl;
            QMessageBox::critical(this,
                   tr("删除失败"),
                   tr("输入用户名不存在"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
}
